//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.netease.nim.demo.session.audio;

import android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.netease.media.e;
import com.netease.nimlib.sdk.media.player.OnPlayListener;

import java.io.File;

import logic.util.logutil.LogUtil;

public final class AudioPlayer2 {
    public static final String TAG = "AudioPlayer2";
    private MediaPlayer mPlayer;
    private String mAudioFile;
    private long mIntervalTime;
    private AudioManager audioManager;
    private OnPlayListener mListener;
    private boolean mCanPlay;
    private int audioStreamType;
    private static final int WHAT_COUNT_PLAY = 0;
    private static final int WHAT_DECODE_SUCCEED = 1;
    private static final int WHAT_DECODE_FAILED = 2;
    private Handler mHandler;
    OnAudioFocusChangeListener onAudioFocusChangeListener;

    public AudioPlayer2(Context var1) {
        this(var1, (String)null, (OnPlayListener)null);
    }

    public AudioPlayer2(Context var1, String var2, OnPlayListener var3) {
        this.mIntervalTime = 500L;
        this.mCanPlay = false;
        this.audioStreamType = 0;
        this.mHandler = new Handler() {
            public void handleMessage(Message var1) {
                switch(var1.what) {
                    case 0:
                        if(AudioPlayer2.this.mListener != null) {
                            AudioPlayer2.this.mListener.onPlaying((long) AudioPlayer2.this.mPlayer.getCurrentPosition());
                        }

                        this.sendEmptyMessageDelayed(0, AudioPlayer2.this.mIntervalTime);
                        return;
                    case 1:
                        AudioPlayer2.this.startInner();
                    default:
                        return;
                    case 2:
                        LogUtil.i("AudioPlayer", "convert() error: " + AudioPlayer2.this.mAudioFile);
                }
            }
        };
        this.onAudioFocusChangeListener = new OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int var1) {
                String var2 = "onAudioFocusChange:" + var1;
                LogUtil.d("Vincent", var2);
                switch(var1) {
                    case -3:
                        if(AudioPlayer2.this.isPlaying()) {
                            AudioPlayer2.this.mPlayer.setVolume(0.1F, 0.1F);
                        }
                        break;
                    case -2:
                        AudioPlayer2.this.stop();
                        return;
                    case -1:
                        AudioPlayer2.this.stop();
                        return;
                    case 0:
                    default:
                        break;
                    case 1:
                        if(AudioPlayer2.this.isPlaying()) {
                            AudioPlayer2.this.mPlayer.setVolume(1.0F, 1.0F);
                            return;
                        }
                }

            }
        };
        this.audioManager = (AudioManager)var1.getSystemService(Context.AUDIO_SERVICE);
        this.mAudioFile = var2;
        this.mListener = var3;
    }

    public final void setDataSource(String var1) {
        if(!TextUtils.equals(var1, this.mAudioFile)) {
            this.mAudioFile = var1;
            this.mCanPlay = !this.needConvert();
        }

    }

    public final void setOnPlayListener(OnPlayListener var1) {
        this.mListener = var1;
    }

    public final OnPlayListener getOnPlayListener() {
        return this.mListener;
    }

    public final void start(int var1) {
        this.audioStreamType = AudioManager.STREAM_MUSIC;
        this.startPlay();
    }

    public final void stop() {
        if(this.mPlayer != null) {
            this.endPlay();
            if(this.mListener != null) {
                this.mListener.onInterrupt();
            }
        }

    }

    public final boolean isPlaying() {
        return this.mPlayer != null && this.mPlayer.isPlaying();
    }

    public final long getDuration() {
        return this.mPlayer != null?(long)this.mPlayer.getDuration():0L;
    }

    public final long getCurrentPosition() {
        return this.mPlayer != null?(long)this.mPlayer.getCurrentPosition():0L;
    }

    public final void seekTo(int var1) {
        this.mPlayer.seekTo(var1);
    }

    private void startPlay() {
        LogUtil.i("AudioPlayer", "start() called");
        this.endPlay();
        if(!this.mCanPlay) {
            Thread var1;
            (var1 = new Thread(new Runnable() {
                public void run() {
                    if(AudioPlayer2.this.mHandler != null) {
                        AudioPlayer2.this.mHandler.sendEmptyMessage(AudioPlayer2.this.convert()?1:2);
                    }

                }
            })).setPriority(10);
            var1.start();
        } else {
            this.startInner();
        }
    }

    private void endPlay() {
        this.audioManager.abandonAudioFocus(this.onAudioFocusChangeListener);
        if(this.mPlayer != null) {
            this.mPlayer.stop();
            this.mPlayer.release();
            this.mPlayer = null;
            this.mHandler.removeMessages(0);
        }

    }

    private boolean needConvert() {
        return VERSION.SDK_INT < 12 && e.b(this.mAudioFile);
    }

    private boolean convert() {
        this.mCanPlay = false;
        String var1;
        if(!TextUtils.isEmpty(var1 = e.a(this.mAudioFile))) {
            this.mCanPlay = true;
            this.mAudioFile = var1;
            this.deleteOnExit();
        }

        return this.mCanPlay;
    }

    private void startInner() {
        this.mPlayer = new MediaPlayer();
        this.mPlayer.setLooping(false);
        this.mPlayer.setAudioStreamType(this.audioStreamType);

        if(audioStreamType == AudioManager.STREAM_MUSIC){
            audioManager.setSpeakerphoneOn(true);
        } else {
            audioManager.setSpeakerphoneOn(false);
        }

        this.audioManager.requestAudioFocus(this.onAudioFocusChangeListener, this.audioStreamType, 2);
        this.mPlayer.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer var1) {
                LogUtil.i("AudioPlayer", "player:onPrepared");
                AudioPlayer2.this.mHandler.sendEmptyMessage(0);
                if(AudioPlayer2.this.mListener != null) {
                    AudioPlayer2.this.mListener.onPrepared();
                }

            }
        });
        this.mPlayer.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer var1) {
                LogUtil.i("AudioPlayer", "player:onCompletion");
                AudioPlayer2.this.endPlay();
                if(AudioPlayer2.this.mListener != null) {
                    AudioPlayer2.this.mListener.onCompletion();
                }

            }
        });
        this.mPlayer.setOnErrorListener(new OnErrorListener() {
            public boolean onError(MediaPlayer var1, int var2, int var3) {
                LogUtil.e("AudioPlayer", "player:onOnError");
                AudioPlayer2.this.endPlay();
                if(AudioPlayer2.this.mListener != null) {
                    AudioPlayer2.this.mListener.onError(String.format("OnErrorListener what:%d extra:%d", new Object[]{Integer.valueOf(var2), Integer.valueOf(var3)}));
                }

                return true;
            }
        });

        try {
            if(this.mAudioFile != null) {
                this.mPlayer.setDataSource(this.mAudioFile);
                this.mPlayer.prepare();
                this.mPlayer.start();
                LogUtil.i("AudioPlayer", "player:start ok---->" + this.mAudioFile);
            } else {
                if(this.mListener != null) {
                    this.mListener.onError("no datasource");
                }

            }
        } catch (Exception var2) {
            var2.printStackTrace();
            LogUtil.e("AudioPlayer", "player:onOnError Exception\n" + var2.toString());
            this.endPlay();
            if(this.mListener != null) {
                this.mListener.onError("Exception\n" + var2.toString());
            }

        }
    }

    private void deleteOnExit() {
        File var1;
        if((var1 = new File(this.mAudioFile)).exists()) {
            var1.deleteOnExit();
        }

    }

    //当前媒体音量
    private static int currVolume = 0;

    //打开扬声器
    public void openSpeaker() {
        try {
            //判断扬声器是否在打开
            audioManager.setMode(AudioManager.ROUTE_SPEAKER);

            //获取当前通话音量
            currVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);

            if (!audioManager.isSpeakerphoneOn()) {
                audioManager.setSpeakerphoneOn(true);

                audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                    audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
                    AudioManager.STREAM_VOICE_CALL);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //关闭扬声器
    public void closeSpeaker() {

        try {
            if(audioManager != null) {
                if(audioManager.isSpeakerphoneOn()) {
                    audioManager.setSpeakerphoneOn(false);
                    audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,currVolume,
                        AudioManager.STREAM_VOICE_CALL);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
